<template>
	<div>
		<!-- Quill 富文本编辑器 -->
		<QuillEditor :style="widths" ref="myQuillEditor" v-model:content="article" theme="bubble" :options="editorOption" contentType="html" />
	</div>
</template>

<script>
	import { QuillEditor } from '@vueup/vue-quill'
	import hljs from 'highlight.js'
	import 'highlight.js/styles/androidstudio.css'

	export default {
		name: 'Reply',
		props: ['content', 'width'],
		computed: {
			widths() {
				return {
					width: this.width,
				}
			},
		},
		components: {
			QuillEditor,
		},
		data() {
			return {
				editorOption: {
					readOnly: true,
					modules: {
						toolbar: null,
						syntax: {
							highlight: text => {
								return hljs.highlightAuto(text).value
							},
						},
					},
				},
				deltaContent: [],
				article: '',
			}
		},
		methods: {
			async get() {
				// 显示回复
				this.deltaContent = this.content
				const quill = this.$refs.myQuillEditor.editor.__quill
				quill.setContents(this.deltaContent)
				const reg = new RegExp(`<pre`, 'g')
				this.article = this.article.replaceAll(reg, '<pre onclick="copy(event.target)"')
			},
			async copy(ele) {
				let str = ele.parentNode.innerText
				const str1 = `//DEXC区块链学院`
				const replaced = str + str1
				await navigator.clipboard.writeText(replaced).then(
					() => {
						this.$message.success('复制成功')
					},
					() => {
						this.$message.error('复制失败！请手动赋值')
					},
				)
			},
		},
		mounted() {
			this.$nextTick(() => {
				this.get()
				window.copy = this.copy
			})
		},
	}
</script>
<style scoped>
	:deep(.ql-container, .ql-snow) {
		font-size: 16px;
		border: 0px;
		cursor: default;
	}

	:deep(.ql-editor) {
		padding: 0;
		cursor: default;
	}
	/* 语法高亮 */
	:deep(.ql-bubble .ql-editor pre) {
		margin: 5px 0 5px;
		position: relative;
		overflow: hidden;
		display: block;
		padding: 30px 8px 2px;
		line-height: 20px;
		word-wrap: break-word;
		text-align: left;
		background: #21252b;
		/* box-shadow: 0 10px 30px 0 rgb(0 0 0 / 40%); */
	}

	/** 3个点 */

	:deep(.ql-bubble .ql-editor pre::before) {
		position: absolute;
		word-wrap: break-word;
		top: 10px;
		left: 15px;
		width: 12px;
		height: 12px;
		font-weight: 700;
		font-size: 12px;
		color: rgb(190, 185, 185);
		line-height: 20px;
		white-space: nowrap;
		text-indent: 750px;
		background-color: #fc625d;
		border-radius: 16px;
		box-shadow: 20px 0 #fdbc40, 40px 0 #35cd4b;
		content: '';
	}

	/* 允许操作 */
	:deep(.ql-syntax) {
		-moz-user-select: text;
		-webkit-user-select: text;
		-ms-user-select: text;
		-khtml-user-select: text;
		user-select: text;
		font-size: 13px;
		overflow: hidden;
		outline: none;
		/* 直接隐藏元素超出的部分 */
	}
	:deep(.ql-editor p img) {
		display: block;
		margin: 0 auto;
	}
</style>
